package com.yssw.project.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.yssw.common.utils.DateUtils;
import com.yssw.product.domain.ProductImage;
import com.yssw.product.mapper.ProductImageMapper;
import com.yssw.project.domain.ProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yssw.project.mapper.ProjectMapper;
import com.yssw.project.domain.Project;
import com.yssw.project.service.IProjectService;

/**
 * 项目信息Service业务层处理
 *
 * @author yssw
 * @date 2023-04-12
 */
@Service
public class ProjectServiceImpl implements IProjectService {
    @Autowired
    private ProjectMapper projectMapper;


    @Autowired
    private ProductImageMapper imageMapper;

    /**
     * 查询项目信息
     *
     * @param id 项目信息主键
     * @return 项目信息
     */
    @Override
    public ProjectVo selectProjectById(Long id) {
        ProjectVo projectVo=new ProjectVo();

        Project project = projectMapper.selectProjectById(id);
        projectVo.setId(project.getId());
        projectVo.setName(project.getName());
        projectVo.setCountry(project.getCountry());
        projectVo.setCity(project.getCity());
        projectVo.setProvince(project.getProvince());
        projectVo.setAddress(project.getAddress());
        projectVo.setLatitude(project.getLatitude());
        projectVo.setDetails(project.getDetails());
        projectVo.setDeviceId(project.getDeviceId());
        projectVo.setDelFlag(project.getDelFlag());
        projectVo.setVersion(project.getVersion());
        projectVo.setDelFlag(project.getDelFlag());
        projectVo.setCreateTime(project.getCreateTime());
        ProductImage productImage = new ProductImage();
        productImage.setProjectId(project.getId());
        projectVo.setImageList(imageMapper.selectProductImageList(productImage));
        return projectVo;
    }

    /**
     * 查询项目信息列表
     *
     * @param project 项目信息
     * @return 项目信息
     */
    @Override
    public List<ProjectVo> selectProjectList(Project project) {
        List<ProjectVo> projectVoList=new ArrayList<>();
        List<Project> projects = projectMapper.selectProjectList(project);
        projects.forEach(pro -> {
            ProjectVo projectVo=new ProjectVo();
            projectVo.setId(pro.getId());
            projectVo.setName(pro.getName());
            projectVo.setCountry(pro.getCountry());
            projectVo.setCity(pro.getCity());
            projectVo.setProvince(pro.getProvince());
            projectVo.setAddress(pro.getAddress());
            projectVo.setLatitude(pro.getLatitude());
            projectVo.setDetails(pro.getDetails());
            projectVo.setDeviceId(pro.getDeviceId());
            projectVo.setDelFlag(pro.getDelFlag());
            projectVo.setVersion(pro.getVersion());
            projectVo.setDelFlag(pro.getDelFlag());
            projectVo.setCreateTime(pro.getCreateTime());
            ProductImage productImage = new ProductImage();
            productImage.setProjectId(pro.getId());
            projectVo.setImageList(imageMapper.selectProductImageList(productImage));
            projectVoList.add(projectVo);
        });
        return projectVoList;
    }

    /**
     * 新增项目信息
     *
     * @param project 项目信息
     * @return 结果
     */
    @Override
    public int insertProject(ProjectVo project) {
        project.setCreateTime(DateUtils.getNowDate());
        int i = projectMapper.insertProject(project);
        if (project.getImageList().size()>0) {
            project.getImageList().forEach(img->{
                img.setProjectId(project.getId());
                img.setCreateTime(DateUtils.getNowDate());
                imageMapper.insertProductImage(img);
            });
        }
        return i;
    }

    /**
     * 修改项目信息
     *
     * @param project 项目信息
     * @return 结果
     */
    @Override
    public int updateProject(ProjectVo project) {
        project.setUpdateTime(DateUtils.getNowDate());
        imageMapper.deleteProductImageByProjectId(project.getId());
        int i = projectMapper.updateProject(project);

        project.getImageList().forEach(img->{
            img.setProjectId(project.getId());
            img.setCreateTime(DateUtils.getNowDate());
            imageMapper.insertProductImage(img);
        });
        return i;
    }

    /**
     * 批量删除项目信息
     *
     * @param ids 需要删除的项目信息主键
     * @return 结果
     */
    @Override
    public int deleteProjectByIds(Long[] ids) {
        return projectMapper.deleteProjectByIds(ids);
    }

    /**
     * 删除项目信息信息
     *
     * @param id 项目信息主键
     * @return 结果
     */
    @Override
    public int deleteProjectById(Long id) {
        return projectMapper.deleteProjectById(id);
    }
}
